package com.jiuyun.Servlet;

import com.google.gson.Gson;
import com.jiuyun.vo.ChartData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 给图表提供json数据
 */
@WebServlet("/echarts")
public class EchartsServlet extends HttpServlet {

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //本应该来自 数据库
        String title[]={"衬衫","群子","雪纺衫","短裤","高跟鞋","袜子"};
        int data[]={10,12,22,40,25,31};
        ChartData cd = new ChartData(title,data);
        String s = gson.toJson(cd);
        //如何让servlet返回json数据
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
        //输出流
        PrintWriter out = response.getWriter();
        out.println(s);  //数据
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
